package cn.com.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiDingDan;
import cn.com.api.entity.ApiT_member_point;
import cn.com.api.entity.ApiT_points_rule;
import cn.com.api.entity.ApiTmember;
import cn.com.api.mapper.ApiT_member_pointMapper;
import cn.com.api.mapper.ApiT_points_ruleMapper;
import cn.com.api.mapper.ApiTmemberMapper;
import cn.com.api.service.ApiT_points_ruleService;
import cn.com.util.*;
import cn.com.util.*;

@Service
public class ApiT_points_ruleServiceImpl implements ApiT_points_ruleService {

	@Autowired
	private ApiT_points_ruleMapper apiT_points_ruleMapper;
	@Autowired
	private ApiTmemberMapper apiTmemberMapper;
	@Autowired
	private ApiT_member_pointMapper apiT_member_pointMapper;

	@Override
	public List<ApiT_points_rule> searchPointsRuleList(Map<String, Object> conditions, PageInfo pageInfo) {
		return apiT_points_ruleMapper.findByConditionsListPage(conditions, pageInfo);
	}

	@Override
	public ApiT_points_rule searchPointsRuleById(String id) {
		ApiT_points_rule rule = new ApiT_points_rule();
		if (!Util.isNull(id)) {
			rule = apiT_points_ruleMapper.searchPointsRuleById(id);
		}
		return rule;
	}

	@Override
	public void deletePointsRuleById(String id) {
		apiT_points_ruleMapper.deletePointsRuleById(id);
	}

	@Override
	public void addOrUpdateRule(ApiT_points_rule t_points_rule) {
		Integer id = t_points_rule.getId();
		if (id != null) {// 编辑
			apiT_points_ruleMapper.updateApiT_points_rule(t_points_rule);
		} else {// 添加
			apiT_points_ruleMapper.insertApiT_points_rule(t_points_rule);
		}
	}

	@Override
	public void awardMemberPoints(ApiDingDan dingDan) {
		if (dingDan != null) {
			// 更新用户积分--订单完成时
			if (0 == dingDan.getOrderType() || 4 == dingDan.getOrderType()) {// 购物订单
				ApiT_member_point apiT_member_point = new ApiT_member_point(); // 1元购
				// 更新用户的积分
				ApiTmember member = apiTmemberMapper.findMemberByCuserid(dingDan.getU_id());
				Map<String, Object> paramData = new HashMap<String, Object>();
				Integer cJf = member.getcJf();
				apiT_member_point.setPoint(cJf);
				if (cJf == null) {
					cJf = 0;
				}
				// --根据下单时间判断当前是否有积分活动 city_id appdate
				List<ApiT_points_rule> list = apiT_points_ruleMapper.searchPointsRuleByOrder(dingDan.getTitles());
				if (list != null && list.size() > 0) {
					cJf = dingDan.getPayMoney().intValue() * list.get(0).getRate().intValue() + cJf;
					apiT_member_point.setChange_point(( dingDan.getPayMoney().intValue()) * list.get(0).getRate());
					apiT_member_point.setConsume_point((dingDan.getPayMoney().intValue()) * list.get(0).getRate());
				} else {
					cJf =  dingDan.getPayMoney().intValue() + cJf;
					apiT_member_point.setChange_point(dingDan.getPayMoney().intValue());
					apiT_member_point.setConsume_point(dingDan.getPayMoney().intValue());
				}
				paramData.put("cUserId", dingDan.getU_id());
				paramData.put("cJf", cJf);
				apiTmemberMapper.modifyApiTmemberByCuserid(paramData);

				// 进入积分明细
				apiT_member_point.setAddtime(Util.CurrentTime());
				apiT_member_point.setRemark("购买商品赠送的积分");
				apiT_member_point.setMember_id(member.getCid().toString());
				apiT_member_point.setType(0);// 收入
				apiT_member_pointMapper.insertT_member_point(apiT_member_point);
			}
		}
	}
}
